package cn.jinjimi.app.main.adapter.recycler;

import android.animation.Animator;
import android.animation.ObjectAnimator;
import android.content.Context;
import android.net.Uri;
import android.text.SpannableStringBuilder;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import com.facebook.imagepipeline.common.ResizeOptions;
import cn.jinjimi.app.R;
import cn.jinjimi.app.entity.Item.HomeMaterialItem;
import cn.jinjimi.app.entity.Material;
import cn.jinjimi.app.main.widget.Fresco.WrapContentDraweeView;
import cn.jinjimi.app.main.widget.FrescoImageView.FrescoImageView;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by Roy
 * Date: 2017/4/10
 */

public class HomeHeadMaterialAdapter extends MyBaseRecyclerAdapter<HomeMaterialItem, HomeHeadMaterialAdapter.ViewHolder> {

    private Context mContext;

    public HomeHeadMaterialAdapter(Context context) {
        this.mContext = context;
    }

    public onActionClickListener onActionClickListener;

    public void setOnActionClickListener(onActionClickListener onActionClickListener) {
        this.onActionClickListener = onActionClickListener;
    }

    public interface onActionClickListener {
        void onItemClick(Material data, int position);
    }

    @Override
    protected Animator[] getAnimators(View view) {
        return new Animator[]{ObjectAnimator.ofFloat(view, "alpha", 0f, 1f)};
    }

    @Override
    public int getItemViewType(int position) {
        return getItem(position).type;
    }

    @Override
    public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        switch (viewType) {
            case HomeMaterialItem.TypeArticle:
                return new ArticleHolder(LayoutInflater.from(mContext).inflate(R.layout.home_head_material_article_item, parent, false));
            case HomeMaterialItem.TypePoster:
                return new PosterHolder(LayoutInflater.from(mContext).inflate(R.layout.home_head_material_poster_item, parent, false));
            case HomeMaterialItem.TypeLogon:
                return new LogionHolder(LayoutInflater.from(mContext).inflate(R.layout.home_head_material_logion_item, parent, false));
            default:
                return new ViewHolder(LayoutInflater.from(mContext).inflate(R.layout.home_head_material_logion_item, parent, false));
        }
    }

    @Override
    public void onBindViewHolder(ViewHolder holder, int position) {
        super.onBindViewHolder(holder, position);

        HomeMaterialItem item = getItem(position);
        holder.setData(item);
    }

    class ViewHolder extends MyBaseRecyclerAdapter.MyViewHolder {
        private View rlContent;

        public ViewHolder(View itemView) {
            super(itemView);
            rlContent = itemView.findViewById(R.id.ll_content);
        }

        public void setData(final HomeMaterialItem item) {
            rlContent.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    if (onActionClickListener != null) {
                        onActionClickListener.onItemClick(item.material, getHolderPosition());
                    }
                }
            });
        }
    }

    private class ArticleHolder extends ViewHolder {

        private FrescoImageView img;
        private TextView tvTitle;
        private ResizeOptions options;

        public ArticleHolder(View itemView) {
            super(itemView);
            img = (FrescoImageView) itemView.findViewById(R.id.img);
            tvTitle = (TextView) itemView.findViewById(R.id.tv_title);
            ViewGroup.LayoutParams params = img.getLayoutParams();
            if (params != null && params.width > 0 && params.height > 0) {
                options = new ResizeOptions(params.width, params.height);
            }
        }

        @Override
        public void setData(HomeMaterialItem item) {
            super.setData(item);
            img.loadView(null, item.material.coverUrl, 0, options);

            SpannableStringBuilder spannableStringBuilder = new SpannableStringBuilder();
            if (item.material.title.length() <= 10) {
                tvTitle.setText(item.material.title);
            } else if (item.material.title.length() > 10 && item.material.title.length() <= 20) {
                spannableStringBuilder.append(item.material.title.substring(0, 10)).append("\n").append(item.material.title.substring(10, item.material.title.length()));
                tvTitle.setText(spannableStringBuilder.toString());
            } else {
                spannableStringBuilder.append(item.material.title.substring(0, 10)).append("\n").append(item.material.title.substring(10, 19)).append("...");
                tvTitle.setText(spannableStringBuilder.toString());
            }
        }
    }

    private class PosterHolder extends ViewHolder {

        private WrapContentDraweeView draweeView;

        public PosterHolder(View itemView) {
            super(itemView);
            draweeView = (WrapContentDraweeView) itemView.findViewById(R.id.img);
        }

        @Override
        public void setData(HomeMaterialItem item) {
            super.setData(item);
            draweeView.setImageURI(Uri.parse(item.material.coverUrl));
        }
    }

    private class LogionHolder extends ViewHolder {
        private FrescoImageView img;
        private ResizeOptions options;

        public LogionHolder(View itemView) {
            super(itemView);
            img = (FrescoImageView) itemView.findViewById(R.id.img);
            ViewGroup.LayoutParams params = img.getLayoutParams();
            if (params != null && params.width > 0 && params.height > 0) {
                options = new ResizeOptions(params.width, params.height);
            }
        }

        @Override
        public void setData(HomeMaterialItem item) {
            super.setData(item);
            img.loadView(null, item.material.coverUrl, 0, options);
        }
    }

    public List<Material> getTargetMaterials(String type) {
        List<Material> materials = new ArrayList<>();
        for (HomeMaterialItem item : getItems()) {
            if (item.material.type.equals(type)) {
                materials.add(item.material);
            }
        }
        return materials;
    }
}
